﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace ClientApplication
{
    class Admin : IAdmin
    {
        public event ContestEndedEventHandler ContestEnded = null;
        public event GetContestantEventHandler GetContestant = null;
        public event JumpDoneEventHandler JumpDone = null;
        //public Result allResults { get; private set; }
        public Contest contest { get; private set; }
        //public Server server { get; private set; }
        //public Result result { get; private set; }

        public Admin()
        {

            contest = new Contest();
            //Thread myNewThread = new Thread(UpdateJudges);
            //myNewThread.Start();
        }
        public void GetActiveContestant()
        {
            Contestant contestant = contest.GetActiveContestant();


            ContestantAddedEventArgs e = new ContestantAddedEventArgs(contestant.sex, contestant.name, contestant.nationality, contestant.age, contestant.id, contestant.jump);

            if (GetContestant != null)
            {
                GetContestant(this, e);
            }


        }
        public void BroadCastJumpCode(string jumpCode, Contestant contestant)
        {

            string stringContestant = contestant.id.ToString() + '@' + jumpCode + '@' + contestant.jump.ToString();


            //Advance active diver, if it returns 1, competition is over.
            if (contest.incrementActiveDiver() == 1)
            {
                if (ContestEnded != null)
                    ContestEnded(this, new EventArgs());

                //-----------------Save old contest to results--------------------------//




                //-------------------------------------------------------------------------

                contest = new Contest();
            }
            //Set jumpDone flag in forms that current jump is done.
            else if (JumpDone != null)
            {
                JumpDone(this, new EventArgs());
            }


        }
        /// <summary>
        /// Inserts contestants from the presenter class to the contestant list.
        /// </summary>
        public void insertContestant(Contestant c)
        {
            contest.contestantList.AddContestants(c);
        }
        public void clearContestant()
        {
            contest.contestantList.contestants.Clear();
        }
        public void clearRules()
        {
            contest.rules.ClearRules();
        }
        public void MakeContest()
        {
            loadContestants();
            //inputJudges();s
            //contest.RunContest();

            //currentContest.PresentResult();
            //LoadContest();
            //SaveContest();
            //allResults.PresentResult();
            Console.ReadLine();
        }
        //Input for rules
        public void inputRules(double height, int amountJudges, int sex, DateTime date, string place)
        {
            Rules rules = new Rules(height, amountJudges, sex, date, place);
            contest.SetUpRules(rules);
        }
        //Manual input for contestant information
        private void loadContestants()
        {

            //While competitors < maxCompetitors//
            ContestantList cList = new ContestantList();
            Contestant c1 = new Contestant(1, "lulu", 20, 0, "Svensk", 0);
            Contestant c2 = new Contestant(2, "lul", 22, 0, "Dansk", 0);
            Contestant c3 = new Contestant(3, "luu", 26, 0, "Norsk", 0);
            cList.AddContestants(c1);
            cList.AddContestants(c2);
            cList.AddContestants(c3);
            int i = 0;
            contest.contestantList.AddContestants(cList[i]);

        }
        public void inputJudges(List<Judge> jList)
        {
            for (int i = 0; i < jList.Count; i++)
			{
                contest.allJudges.Add(jList[i]);
			}
        }
        public void LoadContest()
        {
            //allResults = allResults.LoadResult();
        }
    }
}
